package com.starblink.android.common.view.pop

import android.app.Activity
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import com.starblink.library.widget.novice_guide.control.GuidePopStatusControl
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.ext.margin
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler.trackProductMap
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.common.R
import com.starblink.android.common.databinding.PopFindSimilarBinding
import com.starblink.basic.autosize.utils.ScreenUtils
import com.starblink.basic.util.CommUtils
import com.starblink.rocketreserver.fragment.ProductF

object FindSimilarPopView {

    private var popupWindow: PopupWindow? = null
    fun showPop(
        context: Activity,
        anchorView: View,
        parent: ViewGroup,
        product: ProductF,
        position: Int, pageName: Int,
        findSimilarCall: () -> Unit,
        AddCall: (view: View) -> Unit,
    ) {
        val collected: Boolean = product.collected == true
        GuidePopStatusControl.getHollowRectF(anchorView, true)?.let { rectF ->
            val popWindowView =
                PopFindSimilarBinding.inflate(LayoutInflater.from(context), null, false)
            popupWindow = PopupWindow(
                popWindowView.root,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ScreenUtils.getRawScreenSize(context)[1],
                true
            )

            popWindowView.iv.margin(topMargin = rectF.top.toInt() + 14.dp)
            popWindowView.ivAdd.setImageResource(if (collected) R.mipmap.icon_added_product else R.mipmap.icon_add_product)

            //覆盖状态栏
            popupWindow?.isClippingEnabled = false
            popupWindow?.isOutsideTouchable = true
            popupWindow?.setBackgroundDrawable(ColorDrawable(CommUtils.getColor(com.starblink.basic.style.R.color.sk_gray900_opacity32)))
            // 设置气泡依赖的布局，以及在依赖的布局的哪儿个位置

            popupWindow?.showAtLocation(parent, Gravity.CENTER, 0, 0)
            popWindowView.llSimilar.setOnClickListener {
                findSimilarCall.invoke()
                popupWindow?.dismiss()
            }
            popWindowView.llSimilar.trackData(
                element = SpmElementDef.POP_FIND_SIMILAR_CLICK,
                page = pageName, onlyHit = true,
                traceInfo = product.traceInfo,
                params = trackProductMap(position, product)
            )
            popWindowView.llAdd.setOnClickListener {
                AddCall.invoke(it)
                popupWindow?.dismiss()
            }

            popWindowView.llAdd.trackData(
                element = SpmElementDef.POP_FIND_SIMILAR_ADD,
                page = pageName,onlyHit = true,
                traceInfo = product.traceInfo,
                params = trackProductMap(position, product).plus(
                    "status" to (if (UserDataCenter.isLogin()) "1" else "0")
                ).plus(
                    "result" to (if (collected) "0" else "1")
                )
            )
            popWindowView.cl.setOnClickListener {
                popupWindow?.dismiss()
            }
        }
    }

}